package cn.wolfcode.controller;

import cn.wolfcode.annotation.LoginAnnotation;
import cn.wolfcode.annotation.ParamAnnotation;
import cn.wolfcode.cache.IStrategyCommentRedisService;
import cn.wolfcode.cache.IUserInfoRedisService;
import cn.wolfcode.domain.*;
import cn.wolfcode.query.TravelQuery;
import cn.wolfcode.service.*;
import cn.wolfcode.util.JsonResult;
import cn.wolfcode.util.UMEditorUploader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.rmi.runtime.NewThreadAction;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private ITravelCommentService travelCommentService;





    @GetMapping("query")
    public Object query(TravelQuery qo){
        Page query = travelService.query(qo);
        return JsonResult.success(query);
    }

    @GetMapping("input")
    public Object input(String id){
        //查询所有目的地
        List<Destination> dests = destinationService.listAll();
        //新建一个map集合把查询好的数据丢进去
        Map<String,Object> map = new HashMap<>();
        //如果有id 查询日记对象给页面,用于编辑时回显
        if (StringUtils.hasLength(id)){
            Travel tv = travelService.get(id);
            map.put("tv",tv);
        }
        map.put("dests",dests);
        return JsonResult.success(map);
    }

    //上传图片
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }
    @LoginAnnotation
    @PostMapping("saveOrUpdate")
    public Object saveOrUpdate(Travel travel,HttpServletRequest request){
        String token = request.getHeader("token");
        UserInfo user = userInfoRedisService.getUserByToken(token);
        travelService.saveOrUpdate(travel,user);
        return JsonResult.success(user.getId());
    }

    @GetMapping("detail")
    public Object detail(String id){
        Travel travel = travelService.get(id);
        return JsonResult.success(travel);
    }

    @GetMapping("getUser")
    public Object getUser(@ParamAnnotation UserInfo userInfo){
        //获取登录对象
        return JsonResult.success(userInfo);
    }

    @LoginAnnotation
    @PostMapping("commentAdd")
    public Object commentAdd(TravelComment travelComment, @ParamAnnotation UserInfo userInfo){
        //属性拷贝
        //参数一: 源对象 参数二:目标对象
        //字段名必须要一致
        BeanUtils.copyProperties(userInfo,travelComment);
        travelComment.setUserId(userInfo.getId());
        travelCommentService.insert(travelComment);
        return JsonResult.success();
    }

    @GetMapping("comments")
    public Object comments(String travelId){
        //查询所有游记评论
        List<TravelComment> travelComment = travelCommentService.findByTravelId(travelId);
        return JsonResult.success(travelComment);
    }





}
